//
//  WmcClassifyAlbumSecondLevelCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/14.
//

import UIKit
import Kingfisher

class WmcClassifyAlbumSecondLevelCell: WmcBaseTableCell {
    // Internal

    override func makeUI() {
        lineView.backgroundColor = .wmc_grayH19_F5F5F5
        
        addSubview(albumImageView)
        addSubview(titleLabel)
        addSubview(descLabel)
        addSubview(arrowImageView)
        addSubview(lineView)
        
        albumImageView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(56)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(albumImageView.snp.right).offset(12)
            make.top.equalTo(20)
            make.height.equalTo(21)
            make.right.equalTo(-40)
        }
        
        descLabel.snp.makeConstraints { make in
            make.left.equalTo(albumImageView.snp.right).offset(12)
            make.bottom.equalTo(-20)
            make.height.equalTo(21)
            make.right.equalTo(-40)
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.right.equalTo(-13)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(13)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(17)
            make.right.equalTo(-17)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }

    // Private

    private lazy var albumImageView: UIImageView = {
        let view = UIImageView()
        return view
    }()

    private lazy var descLabel: UILabel = {
        let label = UILabel()
        label.font = 15.wmc_font
        label.textColor = .wmc_grayH5_606066
        return label
    }()
    
    func bindModel(_ model: WmcMediaPhotographStatisticsModel) {
        var params = ""
        
        if model.resourceType == 0 {
            params = "x-oss-process=image/resize,h_200,m_lfit"
        } else if model.resourceType == 1 {
            params = "x-oss-process=video/snapshot,t_7000,ar_auto,f_jpg,m_fast"
        }
        
        var url = "\(model.url)"
        var symbol = "?"
        if url.contains(symbol) {
            symbol = "&"
        }
        url.append(contentsOf: "\(symbol)\(params)")
        
        if let imgUrl = URL(string: url) {
            let urlParts = model.url.components(separatedBy: "?")
            var key = model.url
            if urlParts.count == 2 {
                key = urlParts[0]
            }
            key.append("/thumb")
            albumImageView.kf.setImage(with: .network(Kingfisher.ImageResource(downloadURL: imgUrl, cacheKey: key)), placeholder: "image_placeholder".wmc_image)
        }
        
        titleLabel.text = model.name
        descLabel.text = "\(model.num)张"
    }
}
